import type { ICaptcha, IUpdateInfo, IUpdatePassword, IUserInfoVo, IUserLogin } from './types/login'
import { http } from '@/http/http'

/**
 * 登录表单
 */
export interface ILoginForm {
  username: string
  password: string
  captchaVerification?: string
  // code: string // 用户输入的验证码内容
  // uuid: string // 后端返回的验证码唯一标识
}

export interface IRegisterForm {
  phoneNumber: string
  code: string
  password: string
  confirmPassword: string
  userName: string
}

export interface IForgetPassForm {
  phoneNumber: string
  code: string
  newPassword: string
  confirmPassword: string
}

export interface IResetPassForm {
  oldPassword: string
  newPassword: string
  confirmPassword: string
}

export interface IResetPhoneForm {
  phoneNumber: string
}

export interface IUpdateUserInfoForm {
  userName?: string
  sex?: number
  avatar?: string
}

/**
 * 获取验证码
 * @returns ICaptcha 验证码
 */
export function getCode() {
  return http.get<ICaptcha>('/captcha/captchaImage')
}

/**
 * 用户登录
 * @param loginForm 登录表单
 */
export function login(loginForm: ILoginForm) {
  // return http.post<IUserLogin>('/system/auth/login', loginForm)
  return http.post<IUserLogin>('/system/auth/login', loginForm)
}

/**
 * 忘记密码
 * @returns ICaptcha 忘记密码
 */
export function forgetPassword(forgetPassForm: IForgetPassForm) {
  return http.post<any>('/wp/user/forget-password', forgetPassForm)
}

/**
 * 提交忘记密码申请
 *  @returns ICaptcha 申请结果
 */
export function forgetPasswordApplication(forgetPassForm: IForgetPassForm) {
  return http.post<any>('/wp/tickets/add', forgetPassForm)
}

/**
 * 修改密码
 * @returns ICaptcha 修改密码
 */
export function resetPassword(resetPassForm: IResetPassForm) {
  return http.post<any>('/system/user/update-password', resetPassForm)
}

/**
 * 修改手机号
 * @returns ICaptcha 修改密码
 */
export function updatePhone(resetPassForm: IResetPhoneForm) {
  return http.post<any>('/wp/user/reset-phone', resetPassForm)
}

/**
 * 获取租户信息
 * @returns ICaptcha 修改密码
 */
export function getTenantInfo(tenantId: number) {
  return http.get<any>(`/wp/tenant/info/${tenantId}`)
}

/**
 * 修改用户信息
 * @returns ICaptcha 修改密码
 */
export function updateUserInfo(updateUserInfo: IUpdateUserInfoForm) {
  return http.post<any>('/wp/user/update-info', updateUserInfo)
}

/**
 * 用户注册
 * @param loginForm 登录表单
 */
export function register(registerForm: IRegisterForm) {
  // return http.post<IUserLogin>('/system/auth/login', loginForm)
  return http.post<any>('/wp/user/register', registerForm)
}

/**
 * 获取用户信息
 */
export function getUserInfo() {
  return http.get<IUserInfoVo>('/system/auth/get-permission-info')
}

/**
 * 退出登录
 */
export function logout() {
  return http.post<void>('/system/auth/logout')
}

/**
 * 修改用户信息
 */
export function updateInfo(data: IUpdateInfo) {
  return http.post('/system/user/update', data)
}

/**
 * 修改用户密码
 */
export function updateUserPassword(data: IUpdatePassword) {
  return http.post('/system/user/update-password', data)
}

/**
 * 获取微信登录凭证
 * @returns Promise 包含微信登录凭证(code)
 */
export function getWxCode() {
  return new Promise<UniApp.LoginRes>((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: res => resolve(res),
      fail: err => reject(new Error(err)),
    })
  })
}

/**
 * 微信登录参数
 */

/**
 * 微信登录
 * @param params 微信登录参数，包含code
 * @returns Promise 包含登录结果
 */
export function wxLogin(data: { code: string }) {
  return http.post<IUserLogin>('/user/wxLogin', data)
}
